<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8" />
		<title>第一节课</title>
		<style type="text/css">
			/*去除原来样式*/
			
			* {
				padding: 0;
				margin: 0;
			}
			
			.container {
				width: 100vw;
				height: 100vh;
				display: flex;
				/*
				 * 将边框设置在中间
				 */
				align-items: center;
				justify-content: center;
			}
			
			canvas {
				width: 1100px;
				height: 500px;
				border: 1px solid #333333;
			}
		</style>
	</head>

	<body>
		<div class="container">
			<canvas id="canvas" width="1100" height="500"></canvas>
			<div id="anniu">
				
			
				<input type="button" id="light" value="变亮"/>
				<input type="button" id="dark" value="变暗"/>
				<br />
				<input type="number" id="input" value="" />
				<br />
				<input type="button" id="trans" value="变透明"/>
				<input type="button" id="transj" value="变不透明"/>
				<br />
				<input type="number" id="input2" value="" />
			</div>
		</div>
	</body>

	<script>
		
		window.onload = function() {
			//let oCanvas = document.getElementById('canvas');
			let oCanvas = document.getElementById("canvas");
			let pen = oCanvas.getContext("2d");

			var image = new Image();
			image.src = '../course06/img.png';
			
			image.onload = function(){
				pen.drawImage(image,0,0,500,500);
				var imageData = pen.getImageData(0,0,500,500)
				var data = imageData.data;
				console.log(data);
				
				//var tra=;
				
				
				
				//alert(num);
				//let num=50;
				
				document.getElementById("light").onclick=function(){
					
					let num=parseInt(document.getElementById("input").value);
					
					for(var i=0;i<data.length;i+=4){
						data[i+0] += num
						data[i+1] += num
						data[i+2] += num
					}
					pen.putImageData(imageData,600,0);
				};
				
				document.getElementById("dark").onclick=function(){
					
					let num=parseInt(document.getElementById("input").value);
					
					for(var i=0;i<data.length;i+=4){
						data[i+0] -= num
						data[i+1] -= num
						data[i+2] -= num
					}
					pen.putImageData(imageData,600,0);
				};
			
				document.getElementById("trans").onclick=function(){
					
					let tra=parseInt(document.getElementById("input2").value);
					
					for(var i=0;i<data.length;i+=4){
						data[i+3] -= tra;
					}
					pen.putImageData(imageData,600,0);
				};
			
				
				document.getElementById("transj").onclick=function(){
					
					let tra=parseInt(document.getElementById("input2").value);
					
					for(var i=0;i<data.length;i+=4){
						data[i+3] += tra;
					}
					pen.putImageData(imageData,600,0);
				};
			
				
			}
			
			
		}
	</script>

</html>